package com.cqie.practicaltraining.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.practicaltraining.config.result.R;
import com.cqie.practicaltraining.domain.City;
import com.cqie.practicaltraining.dto.CityDto;
import com.cqie.practicaltraining.dto.CityInfo;
import com.cqie.practicaltraining.dto.District;
import com.cqie.practicaltraining.dto.Province;
import com.cqie.practicaltraining.service.CityService;
import com.cqie.practicaltraining.mapper.CityMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 9977
 * @description 针对表【city(基础表-省市区)】的数据库操作Service实现
 * @createDate 2024-12-26 10:47:28
 */
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements CityService {
    @Override
    public R getCityIdByName(String cityName) {
        if (cityName == null) return R.Failed("地名不能为空");

        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(City::getName, cityName);

        List<City> cityList = list(wrapper);

        if (cityList == null || cityList.isEmpty()) {
            return R.Failed("该地名不存在");
        } else if (cityList.size() == 1) {
            City city = cityList.get(0);
            return R.Success("该地名存在", city.getCityId());
        } else {
            return R.Failed("无下一级城市");
        }
    }

    @Override
    public R getNameByParentId(String cityName) {
        if (cityName == null) {
            LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(City::getParentId, 0);
            List<City> cityList = list(wrapper);
            if (cityList != null && !cityList.isEmpty()) {
                List<CityDto> cityInfos = cityList.stream()
                        .map(city -> new CityDto(city.getCityId(), city.getName()))
                        .collect(Collectors.toList());
                return R.Success("所有一级城市", cityInfos);
            }
            return R.Failed("暂无城市");
        }
        if (Objects.equals(getCityIdByName(cityName).getMsg(), "该地名不存在") || Objects.equals(getCityIdByName(cityName).getMsg(), "无下一级城市"))
            return R.Failed("无下一级城市或者该地名不存在");
        Long cityId = (Long) getCityIdByName(cityName).getData();
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(City::getParentId, cityId);
        List<City> cityList = list(wrapper);
        if (cityList != null && !cityList.isEmpty()) {
            List<CityDto> cityInfos = cityList.stream()
                    .map(city -> new CityDto(city.getCityId(), city.getName()))
                    .collect(Collectors.toList());
            return R.Success("所有下一级城市", cityInfos);
        }
        return R.Failed("无下一级城市");
    }

    @Override
    public R getAllCity() {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        List<City> provinces = list(wrapper);
        if (provinces != null && !provinces.isEmpty()) {
            List<Province> provinceList = new ArrayList<>();
            for (City provinceCity : provinces) {
                Province province = new Province();
                province.setId(provinceCity.getCityId());
                province.setName(provinceCity.getName());
                // 查询该省份下的所有城市
                LambdaQueryWrapper<City> cityWrapper = new LambdaQueryWrapper<>();
                cityWrapper.eq(City::getParentId, provinceCity.getCityId());
                List<City> cities = list(cityWrapper);
                List<CityInfo> cityInfoList = new ArrayList<>();
                for (City city : cities) {
                    CityInfo cityInfo = new CityInfo();
                    cityInfo.setId(city.getCityId());
                    cityInfo.setName(city.getName());
                    // 查询该城市下的所有区县
                    LambdaQueryWrapper<City> districtWrapper = new LambdaQueryWrapper<>();
                    districtWrapper.eq(City::getParentId, city.getCityId());
                    List<City> districts = list(districtWrapper);
                    List<District> districtInfoList = new ArrayList<>();
                    for (City district : districts) {
                        District districtInfo = new District();
                        districtInfo.setId(district.getCityId());
                        districtInfo.setName(district.getName());
                        districtInfoList.add(districtInfo);
                    }
                    cityInfo.setChildren(districtInfoList);
                    cityInfoList.add(cityInfo);
                }
                province.setChildren(cityInfoList);
                provinceList.add(province);
            }
            return R.Success("查询成功", provinceList);
        } else {
            return R.Failed("没有找到省份信息");
        }
    }

    @Override
    public R getCityNameByCityId(int cityId) {
        LambdaQueryWrapper<City> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(City::getCityId, cityId);
        City city = getOne(wrapper1);
        if (city == null) {
            return R.Failed("该城市不存在");
        }
        Integer parentId = city.getParentId();

        // 使用LinkedList来存储城市名称，便于后续反转
        LinkedList<String> cityNames = new LinkedList<>();
        cityNames.add(city.getName());

        while (parentId != 0) {
            LambdaQueryWrapper<City> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(City::getCityId, parentId);
            City parentCity = getOne(wrapper2);
            cityNames.addFirst(parentCity.getName());
            parentId = parentCity.getParentId();
        }

        // 将城市名称拼接成字符串
        StringBuilder Name = new StringBuilder();
        for (String name : cityNames) {
            if (!Name.isEmpty()) {
                Name.append("/");
            }
            Name.append(name);
        }
        return R.Success("查询成功", Name.toString());
    }
}